package com.ruoyi.web.controller.ingredients;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.ingredients.PartIngredients;
import com.ruoyi.system.domain.ingredients.PartIngredientsException;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ingredientsService.IPartIngredientsExceptionService;
import com.ruoyi.system.service.ingredientsService.IPartIngredientsService;
import com.ruoyi.web.websocket.MyWebSocketHandler;

/**
 * 辅料异常记录Controller
 * 
 * @author ljx
 * @date 2020-07-01
 */
@Controller
@RequestMapping("/system/exception")
public class PartIngredientsExceptionController extends BaseController {
	private String prefix = "system/exception";

	@Autowired
	private IPartIngredientsExceptionService partIngredientsExceptionService;
	@Resource
	private IPartIngredientsService iPartIngredientsService;
	@Resource
	private ISysUserService iSysUserService;
	@Resource
	private MyWebSocketHandler myWebSocketHandler;

	@RequiresPermissions("system:exception:view")
	@GetMapping()
	public String exception() {
		return prefix + "/exception";
	}

	/**
	 * 查询辅料异常记录列表
	 */
	@RequiresPermissions("system:exception:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(PartIngredientsException partIngredientsException) {
		startPage();
		List<PartIngredientsException> list = partIngredientsExceptionService
				.selectPartIngredientsExceptionList(partIngredientsException);
		return getDataTable(list);
	}
	@PostMapping("/selectSubName")
	@ResponseBody
	public AjaxResult selectSubName(Long partId) {
		
		PartIngredients selectPartIngredientsById = iPartIngredientsService.selectPartIngredientsById(partId);
		
		List<PartIngredients> data = iPartIngredientsService.selectPartIngredientsListAllByName(selectPartIngredientsById);
		return AjaxResult.success(data);
	}

	/**
	 * 导出辅料异常记录列表
	 */
	@RequiresPermissions("system:exception:export")
	@Log(title = "辅料异常记录", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(PartIngredientsException partIngredientsException) {
		List<PartIngredientsException> list = partIngredientsExceptionService
				.selectPartIngredientsExceptionList(partIngredientsException);
		ExcelUtil<PartIngredientsException> util = new ExcelUtil<PartIngredientsException>(
				PartIngredientsException.class);
		return util.exportExcel(list, "exception");
	}

	/**
	 * 新增辅料异常记录
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap) {
//		List<PartIngredientsException> exceptions = partIngredientsExceptionService.selectPartIngredientsExceptionList(null);
//		Set<Long> ids = exceptions.stream().map(PartIngredientsException::getPartId).collect(Collectors.toSet());
		List<PartIngredients> ingredients = iPartIngredientsService.selectPartIngredientsListAll();
//		ingredients = ingredients.stream().filter(e -> !ids.contains(e.getId())).collect(Collectors.toList());
		List<SysUser> users = iSysUserService.getAllUser();
		mmap.put("ingredients", ingredients);
		mmap.put("users", users);
		return prefix + "/add";
	}

	/**
	 * 新增保存辅料异常记录
	 */
	@RequiresPermissions("system:exception:add")
	@Log(title = "辅料异常记录", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(PartIngredientsException partIngredientsException) {
		// return AjaxResult.error("ccccc");
		partIngredientsException.setCreateBy(ShiroUtils.getLoginName());
		AjaxResult insertPartIngredientsException = partIngredientsExceptionService
				.insertPartIngredientsException(partIngredientsException);
		List<SysUser> object = (List<SysUser>) insertPartIngredientsException.get("data");
		if (object != null && object.size() > 0) {
			for (SysUser sysUser : object) {
				myWebSocketHandler.sendInfo(sysUser.getUserId().toString(), "公告");
			}
		}

		return insertPartIngredientsException;
	}

	/**
	 * 修改辅料异常记录
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		List<PartIngredients> ingredients = iPartIngredientsService.selectPartIngredientsList(null);
		List<SysUser> users = iSysUserService.getAllUser();
		mmap.put("ingredients", ingredients);
		mmap.put("users", users);
		PartIngredientsException partIngredientsException = partIngredientsExceptionService
				.selectPartIngredientsExceptionById(id);
		mmap.put("partIngredientsException", partIngredientsException);
		return prefix + "/edit";
	}

	/**
	 * 修改保存辅料异常记录
	 */
	@RequiresPermissions("system:exception:edit")
	@Log(title = "辅料异常记录", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(PartIngredientsException partIngredientsException) {
		partIngredientsException.setUpdateBy(ShiroUtils.getLoginName());
		return toAjax(partIngredientsExceptionService.updatePartIngredientsException(partIngredientsException));
	}

	/**
	 * 删除辅料异常记录
	 */
	@RequiresPermissions("system:exception:remove")
	@Log(title = "辅料异常记录", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		AjaxResult deletePartIngredientsExceptionByIds = partIngredientsExceptionService
				.deletePartIngredientsExceptionByIds(ids);
		Map<String, Object> object = (Map<String, Object>) deletePartIngredientsExceptionByIds.get("data");
		if (object != null) {

			Set<Entry<String, Object>> mm = object.entrySet();
			for (Entry<String, Object> entry : mm) {
				String m = entry.getKey();
				myWebSocketHandler.sendInfo(m, "公告");
			}

		}
		return deletePartIngredientsExceptionByIds;
	}
}
